package com.zxy;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2021/1/31
 */
public class ActivitiDemo {



    ProcessEngine processEngine;

    RepositoryService repositoryService;

    RuntimeService runtimeService;

    TaskService taskService;

    @Before
    public void createEngine() {
        this.processEngine = ProcessEngines.getDefaultProcessEngine();
        this.repositoryService = this.processEngine.getRepositoryService();
        this.runtimeService = this.processEngine.getRuntimeService();
        this.taskService = this.processEngine.getTaskService();
    }

    @Test
    public void testDeploy() {
        Deployment evection = repositoryService.createDeployment().addClasspathResource("gateway.bpmn")
                .name("出差with监听").deploy();
        System.out.println(evection.getId());
        System.out.println(evection.getName());
    }

    @Test
    public void deleteDeploy()
    {
        repositoryService.deleteDeployment("22501",true);
    }

    @Test
    public void startProcessInstance()
    {
        Map<String,Object> param=new HashMap<String, Object>();
//        param.put("user","jack");
//        param.put("user","xiaoma");
        param.put("user","xiaowu");
        param.put("manager","lee");
        param.put("master","laoda");
//        ProcessInstance zxy = runtimeService.startProcessInstanceByKey("zxy",param);
        ProcessInstance zxy = runtimeService.startProcessInstanceById("75001", "10002", param);
        System.out.println(zxy.getProcessInstanceId());
        System.out.println(zxy.getProcessDefinitionId());
        System.out.println(zxy.getProcessDefinitionName());
        System.out.println(zxy.getProcessDefinitionKey());

    }

    @Test
    public void queryTask()
    {
        List<Task> xiaowu = taskService.createTaskQuery().taskAssignee("xiaowu").list();
        for(Task task:xiaowu)
        {
            System.out.println(task.getAssignee());
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getName());
            taskService.complete(task.getId());
        }
    }

    @Test
    public void  startGatewayProcessInstance()
    {
        Map<String,Object> param=new HashMap<String, Object>();
//        param.put("user","jack");
//        param.put("user","xiaoma");
        param.put("user","小李");
        param.put("master","大刘");
        param.put("accounter","猜谜");
        ProcessInstance leave = runtimeService.startProcessInstanceById("leave:2:75003", "6666", param);
        System.out.println(leave.getProcessInstanceId());
        System.out.println(leave.getProcessDefinitionId());
        System.out.println(leave.getProcessDefinitionName());
        System.out.println(leave.getProcessDefinitionKey());
    }

    @Test
    public void testGatewayWithVariable()
    {
        Task lee = taskService.createTaskQuery().taskAssignee("小李")
                .processInstanceId("77501").singleResult();
        taskService.complete(lee.getId());
//
//        Task liu = taskService.createTaskQuery().taskAssignee("大刘").processInstanceId("67501")
//                .singleResult();
//        Map<String,Object> params=new HashMap<String, Object>();
//        params.put("b",false);
//        taskService.complete(liu.getId(),params);
    }
}
